package com.stox.shared.core.util;

import java.util.List;

import com.stox.shared.core.model.Bar;
import com.stox.shared.core.model.Range;
import com.stox.shared.core.model.enumeration.BarValue;

public abstract class BarUtil {

	public static Double[] values(final List<Bar> bars, final BarValue barValue) {
		final Double[] result = new Double[bars.size()];
		for (int index = 0; index < result.length; index++) {
			result[index] = barValue.get(bars.get(index));
		}
		return result;
	}

	public static Range range(final List<Bar> bars, final BarValue barValue) {
		final Range range = new Range();
		for (final Bar bar : bars) {
			final double value = barValue.get(bar);
			if (range.getHigh() < value) {
				range.setHigh(value);
			}
			if (range.getLow() > value) {
				range.setLow(value);
			}
		}
		return range;
	}

	public static Bar highest(final List<Bar> bars, final BarValue barValue) {
		Bar highest = null;
		for (final Bar bar : bars) {
			if ((null == highest) || (barValue.get(bar) > barValue.get(highest))) {
				highest = bar;
			}
		}
		return highest;
	}

	public static Bar lowest(final List<Bar> bars, final BarValue barValue) {
		Bar lowest = null;
		for (final Bar bar : bars) {
			if ((null == lowest) || (barValue.get(bar) < barValue.get(lowest))) {
				lowest = bar;
			}
		}
		return lowest;
	}

	public static Range highLowRange(final List<Bar> bars) {
		final Range range = new Range();
		range.setHigh(Double.MIN_VALUE);
		range.setLow(Double.MAX_VALUE);
		for (final Bar bar : bars) {
			range.setHigh(Math.max(range.getHigh(), bar.getHigh()));
			range.setLow(Math.min(range.getLow(), bar.getLow()));
		}
		return range;
	}

}
